﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NLUNZ.UserControls
{
    public partial class BulkAddTracking : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void btnOK_Click(object sender, EventArgs e)
        {
            int TrackingRecord, err, cor = 0;
            bool isCor = true;
            string st = "";

            string opnFileName = fuExcel.FileName;

            bool isExcel = Utility.IsExcel(opnFileName);

            if (isExcel == true)
            {

                string path = Server.MapPath("~/Upload/Tracking/");
                string strNewFile = Utility.NewFileName(opnFileName,path);
                fuExcel.SaveAs(strNewFile);


                DataSet ds = Utility.ExcelToDataSet(strNewFile);

                if (ds.Tables.Count == 0)
                {
                    Literal1.Text = "错误,你所上传的文件没有记录.";
                }
                else
                {
                    DataTable dt = ds.Tables[0];
                    //Tracking tracking = new Tracking();

                    DateTime date;
                    DateTime time;

                    string stDN;
                    string stTN;
                    string[] stDNArray;
                    string[] stTNArray;
                    DataTable dtTemp;
                    string output = string.Empty;
                    string errorMessage = string.Empty;
                    try
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            TrackingRecord = i + 2;

                            if(!String.IsNullOrEmpty(dt.Rows[i][0].ToString()))
                            {
                                string trackingDate = DateTime.Parse(dt.Rows[i][0].ToString()).ToString("yyyy-MM-dd");

                                string trackingTime = DateTime.Parse(dt.Rows[i][1].ToString()).ToString("HH:mm");
                                Literal1.Text += string.Format("td:{0} - tt:{1}|", trackingDate, trackingTime);
                                //tracking.TrackingDateTime =
                                DateTime trackingDT =
                                DateTime.Parse(string.Format("{0} {1}", trackingDate, trackingTime));
                                Literal1.Text += string.Format("datetime:{0}|", trackingDT);
                                //tracking.CompanyCode = 
                                string cCode =
                                dt.Rows[i][3].ToString().Trim().ToUpper();
                                Literal1.Text += string.Format("code:{0}|", cCode);
                                //tra.DelivNum = dt.Rows[i][2].ToString();
                                //tra.TrackNum = dt.Rows[i][4].ToString();

                                stDN = dt.Rows[i][2].ToString();
                                stTN = dt.Rows[i][4].ToString();
                                Literal1.Text += string.Format("devNum:{0}-tracknum:{1}|<br/>", stDN, stTN);
                                //bool hasRecord = false;

                                stDNArray = Regex.Split(stDN, @"\s+");
                                stTNArray = Regex.Split(stTN, @"\s+");

                                for (int n = 0; n < stDNArray.Count(); n++)
                                {
                                    for (int t = 0; t < stTNArray.Count(); t++)
                                    {
                                        Tracking tracking = new Tracking();
                                        tracking.TrackingDateTime = trackingDT;
                                        tracking.CompanyCode = cCode;

                                        tracking.DeliveryNumber = stDNArray[n];
                                        tracking.TrackingNumber = stTNArray[t];

                                        output += string.Format("{0}-{1}-{2}-{3},<br/>", tracking.TrackingDateTime,
                                                                tracking.CompanyCode, tracking.DeliveryNumber,
                                                                tracking.TrackingNumber);

                                        bool trackingExist = Repository.IsTrackingExist(tracking.DeliveryNumber,
                                                                                        tracking.TrackingNumber);

                                        if (cbChange.Checked == false)
                                        {

                                            if (!trackingExist)
                                            {
                                                if (!Repository.InsertTrackingRecord(tracking,out errorMessage))
                                                //if (nluDB.parmsInsertTrackingRecord(tra) == false)
                                                {
                                                    err = TrackingRecord;
                                                    st = st + " " + err.ToString();
                                                    isCor = false;
                                                }
                                                else
                                                {
                                                    cor++;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (trackingExist)
                                            {
                                                if (!Repository.UpdateTrackingRecord(tracking))
                                                //if (nluDB.parmsUpdateTrackingRecordByDelivNum(tra) == false)
                                                {
                                                    err = TrackingRecord;
                                                    st = st + " " + err.ToString();
                                                    isCor = false;
                                                }
                                                else
                                                {
                                                    cor++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        string log = DateTime.UtcNow.AddHours(+12).ToString("yyyy-MM-dd HH:mm:ss") + " 已经将" + cor.ToString() + "条记录写进数据库.";

                        if (isCor == true)
                        {
                            Literal1.Text = log;
                        }
                        else
                        {
                            log = log + "第" + st + "行数据有误,录入失败.<div>" + errorMessage + "</div><div>" + output + "</div>";
                            Literal1.Text += log;
                        }
                    }
                    catch(Exception ex)
                    {
                        Literal1.Text += "错误, 文件数据有误, 录入失败. <div>" + ex.Message + "</div><div>" + ex.StackTrace + "</div><div>" + errorMessage + "</div><div>" + output + "</div>";
                        System.IO.File.Delete(strNewFile);
                    }
                    //GridView1DataBind();
                }
            }
            else
            {
                Literal1.Text = "错误, 只能上传Excel文件.";
            }
        }
    }
}